/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zombiedefense.sound;

import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

/**
 * This class will allow the game to play a variety of
 * sound formats by holding several sound managers and 
 * @author &{user}
 */
public class ZombieDefenseSoundManager{
    
    private static SoundManager[] managers = {
	
	new SoundManager(new AudioFormat(
		22050,
		16,
		1,
		true,
		false),10),
	new SoundManager(new AudioFormat(
		44100,
		16,
		2,
		true,
		false),10),
	new SoundManager(new AudioFormat(
		44100,
		16,
		1,
		true,
		false),10),
	new SoundManager(new AudioFormat(
		22050,
		16,
		2,
		true,
		false
		),10)
	
    };
    
    public static InputStream playSound(String name){
	return play("/sounds/" + name + ".wav");
    }
    
    public static InputStream playMusic(String name){
	return play("/music/" + name + ".wav");
    }
    
    private static InputStream play(String path){
	try{
	    //open input stream
	    AudioInputStream istream = AudioSystem.getAudioInputStream(ZombieDefenseSoundManager.class.getResource(path));
	    AudioFormat format = istream.getFormat();
	    
	    for(SoundManager s: managers){
		if(s.getPlaybackFormat().matches(format)){
		    s.play(istream);
		    return istream;
		}
	    }
	    
	    System.out.println("could not play sound: " + path + "\nof format: " + istream.getFormat().toString());
	    
	}catch(Exception e){
	    System.out.println("could not open sound file: " + path);
	}
	
	return null;
    }
    
}
